package com.iye.puretennis.activity.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import com.iye.puretennis.R;
import com.iye.puretennis.utils.StringUtils;

import java.util.Calendar;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/11/20
 * Time: 9:27
 */
public class TimePeriodActivity extends Activity {
    private int year;
    private int month;
    private int day;
    private int hourFrom;
    private int minuteFrom;
    private int hourTo;
    private int minuteTo;
    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeperiod);
        DatePicker date = (DatePicker) findViewById(R.id.date_picker);
        date.setCalendarViewShown(false);
        TimePicker timeFrom = (TimePicker) findViewById(R.id.time_picker_from);
        timeFrom.setIs24HourView(true);
        TimePicker timeTo = (TimePicker) findViewById(R.id.time_picker_to);
        timeTo.setIs24HourView(true);
        // 获取当前的年月日，小时，分钟
        Calendar ca = Calendar.getInstance();
        year = ca.get(Calendar.YEAR);
        month = ca.get(Calendar.MONTH) + 1;
        day = ca.get(Calendar.DAY_OF_MONTH);
        hourFrom = hourTo = ca.get(Calendar.HOUR);
        minuteFrom = minuteTo = ca.get(Calendar.MINUTE);

        // 初始化DatePicker
        date.init(year, month, day, new DatePicker.OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker arg0, int year, int month, int day) {
                TimePeriodActivity.this.year = year;
                TimePeriodActivity.this.month = month + 1;
                TimePeriodActivity.this.day = day;
                // 显示当前时间和日期
                showDateTime(year, month + 1, day, hourFrom, minuteFrom, hourTo, minuteTo);
            }

        });

        // 为TimerPicker指定事件监听器
        timeFrom.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                if (hour > hourTo || (hour == hourTo && minute > minuteTo)) {
                    showDialog(TimePeriodActivity.this, null, "开始时间必须小于结束时间！");
                    timePicker.setCurrentHour(hourFrom);
                    timePicker.setCurrentMinute(minuteFrom);
                } else {
                    TimePeriodActivity.this.hourFrom = hour;
                    TimePeriodActivity.this.minuteFrom = minute;
                    // 显示当前时间和日期
                    showDateTime(year, month, day, hourFrom, minuteFrom, hourTo, minuteTo);
                }
            }
        });

        timeTo.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {
                if (hourFrom > hour || (hourFrom == hour && minuteFrom > minute)) {
                    showDialog(TimePeriodActivity.this, null, "开始时间必须小于结束时间！");
                    timePicker.setCurrentHour(hourTo);
                    timePicker.setCurrentMinute(minuteTo);
                } else {
                    TimePeriodActivity.this.hourTo = hour;
                    TimePeriodActivity.this.minuteTo = minute;
                    // 显示当前时间和日期
                    showDateTime(year, month, day, hourFrom, minuteFrom, hourTo, minuteTo);
                }

            }
        });

        showDateTime(year, month, day, hourFrom, minuteFrom, hourTo, minuteTo);

    }

    protected void showDateTime(int year, int month, int day, int hourFrom, int minuteFrom, int hourTo, int minuteTo) {
        EditText text = (EditText) findViewById(R.id.show_time);
        text.setText(year + "年" + month + "月" + day + "日 " + hourFrom + ":" + minuteFrom + " - " + hourTo + ":" + minuteTo);
    }

    private void showDialog(Context context, String title, String message) {
        if (builder == null) {
            builder = new AlertDialog.Builder(context);
            if (StringUtils.hasText(title)) {
                builder.setTitle(title);
            }
            DialogInterface.OnClickListener dialogDismissListener = new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                    builder = null;
                }
            };
            builder.setMessage(message);
            builder.setPositiveButton("确定", dialogDismissListener);
            builder.show();
        }
    }
}
